package org.example.lc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class p49 {
    class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            char[][] sorted = new char[strs.length][];
            for(int i=0;i<strs.length;i+=1) {
                char[] data = strs[i].toCharArray();
                Arrays.sort(data);
                sorted[i] = data;
            }
            List<List<String>> result = new ArrayList<>();
            List<String> temp = new ArrayList<>();
            for(int i = 0;i<strs.length;i+=1) {
                if(strs[i]==null) {
                    continue;
                }
                temp.add(strs[i]);
                for(int j=i+1;j<strs.length;j+=1) {
                    if(Arrays.equals(sorted[i] ,sorted[j])) {
                        temp.add(strs[j]);
                        strs[j] = null;
                    }
                }
                result.add(temp);
                temp = new ArrayList<>();
            }
            return result;
        }
    }
    @Test
    public void d1() {
        List<List<String>> lists = new Solution().groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"});
        System.out.println(lists);
        lists.stream().forEach(i-> System.out.println(i));
    }

}
